<?php
session_start();
include('lib/lib.php');
include('connado.php');
//include('authen.php');
header("Content-type: text/xml; charset=utf-8");
$logfile = PAHTLOG."ajax_dayalarmemp-" . date("Y-m-d") . ".log";

$emp_id = $_REQUEST['emp_id'];
$day1 = $_REQUEST['day1'];
$time_b1 = $_REQUEST['time_b1'];
$time_e1 = $_REQUEST['time_e1'];
$day2 = $_REQUEST['day2'];
$time_b2 = $_REQUEST['time_b2'];
$time_e2 = $_REQUEST['time_e2'];
$day3 = $_REQUEST['day3'];
$time_b3 = $_REQUEST['time_b3'];
$time_e3 = $_REQUEST['time_e3'];
$day4 = $_REQUEST['day4'];
$time_b4 = $_REQUEST['time_b4'];
$time_e4 = $_REQUEST['time_e4'];
$day5 = $_REQUEST['day5'];
$time_b5 = $_REQUEST['time_b5'];
$time_e5 = $_REQUEST['time_e5'];
$day6 = $_REQUEST['day6'];
$time_b6 = $_REQUEST['time_b6'];
$time_e6 = $_REQUEST['time_e6'];
$day7 = $_REQUEST['day7'];
$time_b7 = $_REQUEST['time_b7'];
$time_e7 = $_REQUEST['time_e7'];
$action = $_REQUEST['action'];


writelog($logfile, "emp_id     : " . $emp_id);
writelog($logfile, "day1[$day1][$time_b1][$time_e1]");
writelog($logfile, "day2[$day2][$time_b2][$time_e2]");
writelog($logfile, "day3[$day3][$time_b3][$time_e3]");
writelog($logfile, "day4[$day4][$time_b4][$time_e4]");
writelog($logfile, "day5[$day5][$time_b5][$time_e5]");
writelog($logfile, "day6[$day6][$time_b6][$time_e6]");
writelog($logfile, "day7[$day7][$time_b7][$time_e7]");

$sql = "select count(*) calarm from req_alarm_spacial where emp_id=?";
$stmt = $db->Prepare($sql);
$arr_pre = array($emp_id);
$rs = $db->Execute($stmt, $arr_pre);
if($arr = $rs->FetchRow()){
	$calarm = $arr['calarm'];
	$rs->close();		
}
if($action == "edit"){
	
	if($calarm > 0){//delete & insert
	
		$db->BeginTrans(); 
		$db->Execute("delete from req_alarm_spacial where emp_id = $emp_id");
		$sql = "insert into work_day(emp_id, day_work, time_work_b, time_work_e, status_work)values('$emp_id', 1, '$time_b1', '$time_e1', '$day1')";		
		$db->Execute($sql);
		$sql = "insert into work_day(emp_id, day_work, time_work_b, time_work_e, status_work)values('$emp_id', 2, '$time_b2', '$time_e2', '$day2')";		
		$db->Execute($sql);
		$sql = "insert into work_day(emp_id, day_work, time_work_b, time_work_e, status_work)values('$emp_id', 3, '$time_b3', '$time_e3', '$day3')";		
		$db->Execute($sql);
		$sql = "insert into work_day(emp_id, day_work, time_work_b, time_work_e, status_work)values('$emp_id', 4, '$time_b4', '$time_e4', '$day4')";		
		$db->Execute($sql);
		$sql = "insert into work_day(emp_id, day_work, time_work_b, time_work_e, status_work)values('$emp_id', 5, '$time_b5', '$time_e5', '$day5')";		
		$db->Execute($sql);
		$sql = "insert into work_day(emp_id, day_work, time_work_b, time_work_e, status_work)values('$emp_id', 6, '$time_b6', '$time_e6', '$day6')";		
		$db->Execute($sql);
		$sql = "insert into work_day(emp_id, day_work, time_work_b, time_work_e, status_work)values('$emp_id', 7, '$time_b7', '$time_e7', '$day7')";		
		$db->Execute($sql);	
		$db->CommitTrans(); 
		
	}else{// insert 
		$db->BeginTrans(); 
		$sql = "insert into work_day(emp_id, day_work, time_work_b, time_work_e, status_work)values('$emp_id', 1, '$time_b1', '$time_e1', '$day1')";		
		$db->Execute($sql);
		$sql = "insert into work_day(emp_id, day_work, time_work_b, time_work_e, status_work)values('$emp_id', 2, '$time_b2', '$time_e2', '$day2')";		
		$db->Execute($sql);
		$sql = "insert into work_day(emp_id, day_work, time_work_b, time_work_e, status_work)values('$emp_id', 3, '$time_b3', '$time_e3', '$day3')";		
		$db->Execute($sql);
		$sql = "insert into work_day(emp_id, day_work, time_work_b, time_work_e, status_work)values('$emp_id', 4, '$time_b4', '$time_e4', '$day4')";		
		$db->Execute($sql);
		$sql = "insert into work_day(emp_id, day_work, time_work_b, time_work_e, status_work)values('$emp_id', 5, '$time_b5', '$time_e5', '$day5')";		
		$db->Execute($sql);
		$sql = "insert into work_day(emp_id, day_work, time_work_b, time_work_e, status_work)values('$emp_id', 6, '$time_b6', '$time_e6', '$day6')";		
		$db->Execute($sql);
		$sql = "insert into work_day(emp_id, day_work, time_work_b, time_work_e, status_work)values('$emp_id', 7, '$time_b7', '$time_e7', '$day7')";		
		$db->Execute($sql);	
		$db->CommitTrans(); 
		
	}
}else if($action == "find"){
	$xml = "";
	if($calarm > 0){
		$xml .= "<result>";		
		$sql = "select * from work_day where emp_id=? order by day_work asc";
		$stmt = $db->Prepare($sql);
		$arr_pre = array($emp_id);
		$rs = $db->Execute($stmt, $arr_pre);
		$i = 1;
		while($arr = $rs->FetchRow()){
			$xml .= "<day$i>".$arr['status_work']."</day$i><time_b$i>".$arr['time_work_b']."</time_b$i><time_e$i>".$arr['time_work_e']."</time_e$i>";
			$i++;
		}
		
		$xml .= "</result>";		
	}else{
		$xml .=  "<result><day1></day1><time_b1></time_b1><time_e1></time_e1><day2></day2><time_b2></time_b2><time_e2></time_e2>";
		$xml .= "<day3></day3><time_b3></time_b3><time_e3></time_e3><day4></day4><time_b4></time_b4><time_e4></time_e4>";
		$xml .= "<day5></day5><time_b5></time_b5><time_e5></time_e5><day6></day6><time_b6></time_b6><time_e6></time_e6>";
		$xml .= "<day7></day7><time_b7></time_b7><time_e7></time_e7>";
		$xml .= "</result>";
	}
}

writelog($logfile, "reply : $xml");
writelog($logfile, "-------------------------------------------------- ");

echo $xml;
?>

